package com.atguigu.gulimall.product.service.impl;

import cn.hutool.core.stream.CollectorUtil;
import com.sun.org.apache.regexp.internal.RE;
import io.prometheus.client.Collector;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 查出所有的分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);

        // 组装父子结构
        // 一级分类
        List<CategoryEntity> collect = categoryEntities.stream().filter(c -> c.getParentCid() == 0)
                .map(item -> {
                    item.setChildren(getChildrens(item,categoryEntities));
                    return item;
                }).sorted((o1,o2)->{
                    return  (o1.getSort()==null?0:o1.getSort()) - (o2.getSort()==null?0:o2.getSort());
                })
                .collect(Collectors.toList());



        return categoryEntities;
    }

    @Override
    public void removeMenusByIds(List<Long> asList) {
        //TODO  判断是否被其他地方引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long>  resList  = new ArrayList<>();
        resList= findParentCid(catelogId,resList);
        Collections.reverse(resList);
        Long[] res = resList.toArray(new Long[resList.size()]);
        return res;
    }

    private List<Long>  findParentCid(Long catelogId,List<Long> resList){
        resList.add(catelogId);
        CategoryEntity category = this.getById(catelogId);
        if(category.getParentCid()!=0){
            findParentCid(category.getParentCid(),resList);
        }
        return resList;
    }

    List<CategoryEntity> getChildrens(CategoryEntity entity,List<CategoryEntity> list){

        List<CategoryEntity> collect = list.stream().filter(c -> c.getParentCid()==(entity.getCatId()))
                .map(item -> {
                    item.setChildren(getChildrens(item, list));
                    return item;
                }).sorted((o1, o2) -> {
                    return (o1.getSort()==null?0:o1.getSort()) - (o2.getSort()==null?0:o2.getSort());
                }).collect(Collectors.toList());

        return  collect;
    }



}
